# coding=UTF-8
__author__ = 'Administrator'
import re


def process_time(time):
    if ':' in time:
        min = re.search(r'(\d*):(\d*)', time).group(1)
        sec = re.search(r'(\d*):(\d*)', time).group(2)
    elif '分' in time and '秒' in time:
        min = re.search(r'(\d*)分(\d*)秒', time).group(1)
        sec = re.search(r'(\d*)分(\d*)秒', time).group(2)
    elif '.' in time and '"' in time:
        min = 0
        sec = re.search(r'(\d*)\.', time).group(1)
    elif time.isdigit():
        min = 0
        sec = time
    return int(min) * 60 + int(sec)


def get_highest_score_player(score_event, time1='12:00', time2='0.0"'):
    '''
    得到time1-time2时间内最高得分球员
    :param score_event: 输入处理好的得分事件
    :param time1: 开始时间
    :param time2: 结束时间
    :return: 球员信息
    '''
    d = {}
    for s in score_event:
        if process_time(time1) >= process_time(s[-1]) and process_time(time2) <= process_time(s[-1]):
            if s[1] in d:  ##lynn
                d[s[1]] += s[4]
            else:
                d[s[1]] = s[4]
    dict_s = sorted(d.items(), key=lambda d: d[1], reverse=True)  ##lynn
    return dict_s, d


def get_continue_score_player(score_event, time1='12:00', time2='0.0"', score_floor=4):
    continue_score_player = {}
    continue_score_player_sorted = None
    pre = score_event[0][1]
    continue_score = score_event[0][4]
    score_type = [score_event[0][3]]
    score_time = [score_event[0][-1]]
    game_point = [score_event[0][-2]]
    for s in score_event[1:]:
        if process_time(time1) >= process_time(s[-1]) and process_time(time2) <= process_time(s[-1]):
            if s[1] == pre:
                continue_score += s[4]
                score_type.append(s[3])
                score_time.append(s[-1])
                game_point.append(s[-2])
            else:
                if continue_score >= score_floor:
                    continue_score_player[pre] = [continue_score, score_type, score_time, game_point]
                continue_score = s[4]
                score_type = [s[3]]
                score_time = [s[-1]]
                game_point = [s[-2]]
            pre = s[1]
    if len(continue_score_player) > 0:
        continue_score_player_sorted = sorted(continue_score_player.items(), key=lambda d: d[1][0],
                                              reverse=True)  ##lynn

    return continue_score_player_sorted, continue_score_player


def get_score_difference(score_event, time1='12:00', time2='0.0"'):
    score_a1 = 0
    score_b1 = 0
    score_a2 = 0  ##lynn
    score_b2 = 0  ##lynn
    for s in score_event:
        if s[-1] == time1:
            score_a1 = int(s[-2].split('-')[0])
            score_b1 = int(s[-2].split('-')[1])
        if s[-1] == time2:
            score_a2 = int(s[-2].split('-')[0])
            score_b2 = int(s[-2].split('-')[1])
    if time1 == '12:00':
        score_a1 = int(score_event[0][-2].split('-')[0])
        score_b1 = int(score_event[0][-2].split('-')[1])
    if time2 == '0.0"':
        score_a2 = int(score_event[-1][-2].split('-')[0])
        score_b2 = int(score_event[-1][-2].split('-')[1])
    return score_a2 - score_a1, score_b2 - score_b1


def get_cumulate_data():
    # TODO 得到某段时间内球员积累的数据
    return
